package com.kyexpress.common.hrinterview.provider.controller;

import com.kyexpress.common.hrinterview.api.bo.DimensionCodeBO;
import com.kyexpress.common.hrinterview.api.model.EmployeeUpload;
import com.kyexpress.common.hrinterview.provider.service.EmployeeUploadService;
import com.kyexpress.common.hrinterview.provider.utils.ErrorEnum;
import com.kyexpress.common.hrinterview.provider.utils.Tools;
import com.kyexpress.common.hrinterview.provider.utils.TwoDimensionCode;
import com.kyexpress.common.idcenter.IdGenClient;
import com.kyexpress.framework.model.ResponseData;
import com.kyexpress.framework.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;

/**
 *  生成二维码
 */
@RestController
public class DimensionCodeController {
    private static final Logger logger = LoggerFactory.getLogger(DimensionCodeController.class);
    @Autowired
    private EmployeeUploadService employeeUploadService;
    @RequestMapping(value = "/dimensionCode/encoder", method = RequestMethod.POST)
    public ResponseData encoderQRCode(@RequestBody DimensionCodeBO bo){
        Long employeeId = bo.getEmployeeid();
        String type = bo.getTypeName();
        String content = bo.getContent();
        ResponseData result = new ResponseData();
        String fileName = Tools.randomUUID()+".png";
        if(StringUtils.isNotEmpty(type) && employeeId != null && StringUtils.isNotEmpty(content)){
            String imgPath = Tools.FILE_PATH + employeeId+"/"+fileName;
            File dest = new File(imgPath);
            /**
             * 检测是否存在目录
             */
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
//            String encoderContent = Tools.getRandomString(4);
            TwoDimensionCode code = new TwoDimensionCode();
//            code.encoderQRCode(encoderContent, imgPath, "png");
            code.encoderQRCode(content, imgPath, "png");
            EmployeeUpload vo = new EmployeeUpload();
            vo.setTypeName(type);
            vo.setEmployeeid(employeeId);
            vo.setId(IdGenClient.getId());
            vo.setFileName("QRCode.png");
            vo.setFileUrl("/" + employeeId+"/"+fileName);
            this.employeeUploadService.saveDimensionCode(vo);
        }else{
            ErrorEnum.DimensionCodeAndEmployeeIdNotNull.initResponseData(result);
            return result;
        }
        result.setData(fileName).ok();
        return result;
    }
}
